
const { parentPort } = require('worker_threads');
import { DataHandler } from '../../trader/DataHandler';
import { transformToCSV } from '../../utils';

if (parentPort) {
  // 在主进程或主线程中
  parentPort.on('message', msg => {
   const result = toPeriodData(msg);
   parentPort.postMessage(result);
 });
} else {
  process.on("message", msg => {
    // 执行任务
    const result = toPeriodData(msg);
    // 将结果发送给主线程
    process.send(result);
    process.exitCode = 1;
  });
}



export function toPeriodData({period, history}) {
  if (period === "4h") {
    const dataHander = new DataHandler('mock');
    const periodList = [];
    dataHander.on("interval:4h", data => {
      periodList.push(data);
    });
    history.forEach(item => {
      dataHander.handleHistory(item);
    });

    return transformToCSV(periodList);
  }
  if (period === "1d") {
    const dataHander = new DataHandler('mock');
    const periodList = [];
    dataHander.on("interval:1d", data => {
      periodList.push(data);
    });
    history.forEach(item => {
      dataHander.handleHistory(item);
    });
    return transformToCSV(periodList);
  }
  return transformToCSV(history);
}
